import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../../function';
import {
  startReadCpbMail,
  getStockCpbList,
  getOptionCpbLis,
  getCpbSumIndustry,
} from '../../../../services/cqbposition_service';
import { stockIndustryPer } from '../../../../services/allposition_service';

export default {
  namespace: 'cpb',
  state: {
    stocks: { code: 0, message: '', list: [] },
    stocksFilters: {},
    options: { code: 0, message: '', list: [] },
    optionsFilters: {},
    fundPieData: {
      code: '',
      message: '',
      list: {
        title: '',
        date: '',
        items: [],
      },
    },
  },

  effects: {
    // 读取
    *StartReadCpbMail({ payload, callback }, { call, put }) {
      const response = yield call(startReadCpbMail, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // options
    *GetOptionCpbLis({ payload, callback }, { call, put }) {
      const response = yield call(getOptionCpbLis, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveOptions',
          payload: {
            list: { code: 1, message: response.message, list: response.list },
            filter: {},
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // stocks
    *GetStockCpbList({ payload, callback }, { call, put }) {
      const response = yield call(getStockCpbList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveStocks',
          payload: {
            list: { code: 1, message: response.message, list: response.list },
            filter: {},
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // stocks
    *GetCpbSumIndustry({ payload, callback }, { call, put }) {
      const response = yield call(getCpbSumIndustry, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFundPieData',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload.list,
        stocksFilters: action.payload.filter,
      };
    },

    saveOptions(state, action) {
      return {
        ...state,
        options: action.payload.list,
        optionsFilters: action.payload.filter,
      };
    },

    saveFundPieData(state, action) {
      return {
        ...state,
        fundPieData: action.payload,
      };
    },
  },
};
